/**
 * 
 */
package com.changil.nowonchangilchurch.protocol.http;

import android.content.Context;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * @author zeropol2
 * 
 */
public class RestClient {
	private final static int TIMEOUT = 7*1000;
	public static final String BASE_URL = "http://changil153.vps.phps.kr/NowonChangilChurch_Server/api";

	private static AsyncHttpClient client = new AsyncHttpClient();

	public static void get(String url, AsyncHttpResponseHandler responseHandler) {
		client.setTimeout(TIMEOUT);
		client.get(getAbsoluteUrl(url), responseHandler);
	}

	public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		client.setMaxRetriesAndTimeout(3, TIMEOUT);
		client.post(getAbsoluteUrl(url), params, responseHandler);
	}
	
	public static void put(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		client.setMaxRetriesAndTimeout(3, TIMEOUT);
		client.put(getAbsoluteUrl(url), params, responseHandler);
	}
	
	public static void delete(String url, AsyncHttpResponseHandler responseHandler) {
		client.setMaxRetriesAndTimeout(3, TIMEOUT);
		client.delete(getAbsoluteUrl(url), responseHandler);
	}
	
	public static void cancel(Context context, boolean mayInterruptIfRunning) {
		client.setMaxRetriesAndTimeout(3, TIMEOUT);
		client.cancelRequests(context, mayInterruptIfRunning);
	}

	private static String getAbsoluteUrl(String relativeUrl) {
		return BASE_URL + relativeUrl;
	}

}
